package com.cmcc.xdr.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Util {

	private static Logger logger = LogManager.getLogger(Util.class);
	public static boolean checkNotNull(final Object value) {
		if (value == null) {
			return false;
		}
		return (value.toString().length() > 0);
	}

	public static boolean checkNull(final Object value) {
		if (value == null) {
			return true;
		}

		return (value.toString().length() == 0);

	}
	
	public static String sqlZhuanyi(String val){
		try {
			if(val.indexOf("'") != -1){
				val = val.replaceAll("'", "\\\\'");
			}
			if(val.indexOf("\\\\") != -1){
				val = val.replaceAll("\\", "\\\\\\\\");
			}
			if(val.indexOf("%") != -1){
				val = val.replaceAll("%", "\\%");
			}
			if(val.indexOf("_") != -1){
				val = val.replaceAll("_", "\\_");
			}
			
		} catch (Exception e) {
			logger.error(val+"：--字符串转义出现异常：",e);
			return "";
		}
		return val;
	}
	
	public static String sqlsub(String val,int len){
		if(val.length() > len){
			val = val.substring(0, len);
		}
		return val;
	}
	
	public static String splitLastNum(int num){
		String s=String.valueOf(num);
		return s.substring(0, s.length()-1); 
		
	}
	/**
	 * 两个时间比较舍弃精度比较
	 * 例如1234和1235比较，相差1毫秒
	 * @param start
	 * @param end
	 * @return  true 和 false
	 */
	public static Boolean JudgementTime(int start,int end){
		//舍弃的精度值
		int precision=5;
		return Math.abs(start-end)<precision; 
		
	}
	
	/**
	 * 处理IPV6 中的格式，0 换成空格
	 * @param 带：0： 的ipv6地址
	 * @return  0 替换成空格的ipv6地址
	 */
	public static String ipv6EmptyByZero(String ipv6Before){
		String dataUserIp ="";
//		if(ipv6Before.contains(".")){
//			ipv6Before=ipv6Before.replaceAll(".", ":");
//		}
		if(ipv6Before.contains(":0:")){
			dataUserIp = ipv6Before.replaceAll(":0:", ":");
			dataUserIp =dataUserIp.replace(":0:", "::");
		}else{
			dataUserIp =ipv6Before;
		}
		return dataUserIp;
		
	}
	
	public static boolean ipAddressEquals(String ipBefore,String ipAfter){
		if(Util.ipv6EmptyByZero(ipBefore).equals(Util.ipv6EmptyByZero(ipAfter))){
			return true;
		} 
		return false;
	}
	
	
	public static void main(String[] args) {
		String str = "12345\\abc12456";
		System.out.println("替换前："+str);
		if(str.indexOf("\\\\") != -1){
			str = str.replaceAll("\\\\", "\\\\\\\\");
		}
		System.out.println("替换后："+str);
		System.out.println(JudgementTime(123,1252));
		
		boolean ss= Util.ipAddressEquals("2409:8018:800:9:0:0:1:6","2409.8018.800.9.0.0.1.6");
		System.out.println(ss);
		JudgementTime(492,493);
		System.out.println(JudgementTime(492,493));
	}
}
